package com.neusoft.liduan.chess;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 	在Java中的图形界面开发，使用的是Swing
 * 		常用类JFrame(窗口)、面板(JPanel)
 *		MyFrame默认是隐藏的窗口
 */
public class MyFrame extends JFrame{
	public static void main(String[] args) {
		MyFrame jf = new MyFrame();
		//设置窗口的大小
		jf.setSize(400, 500);
		//设置标题
		jf.setTitle("东软中国象棋");
		//关闭时同时关闭虚拟机
		jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
		GamePanel p = new GamePanel();
		//添加窗口中
		jf.add(p);
		
		//显示窗口，建议放在最后一行
		jf.setVisible(true);
	}
	
	private static class GamePanel extends JPanel {
		private List<Chess> chesses = new ArrayList<Chess>();
		
		public GamePanel() {
			initChess();
			addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					int x = e.getX();
					int y = e.getY();
					System.out.println("当前点击的坐标x和y" + x + "," + y);
					Point p = Chess.getPointFromXY(x, y);
					System.out.println("点击的坐标为Point为" + p);
					for(int i = 0; i < chesses.size(); i++) {
						Chess item = chesses.get(i);
						if(item.getP().equals(p)) {
							System.out.println("当前点击的棋子为" + item);
							item.setP(new Point(1, 5));
							repaint();
						}
					}
				}
			});
		}
		
		@Override//注解，表示该方法是一个重写方法，不是方法重写不可以加
		public void paint(Graphics g) {
			super.paint(g);
			System.out.println("paint");
			/*	Graphics：画笔
			 * 		JPanel：类似于画布
			 * 		g.drawImage(img, x, y, observer)
			 * 			img：是一Image对象，即图片对象
			 * 			x：坐标x
			 * 			y：坐标y
			 * 			observer：观察者，暂时不用管，直接写this
			 * 		g.drawImage(img, x, y, width, height, observer);
			 * 			width：指定要画的图片宽度
			 * 			height：高度
			 * 
			 */
			//根据路径获取图片对象
			Image bg = Toolkit.getDefaultToolkit().getImage("pic/qipan.jpg");
			g.drawImage(bg, 0, 0, this);
//			Image che0 = Toolkit.getDefaultToolkit().getImage("pic/che0.png");
//			g.drawImage(che0, 5, 5, 30, 30, this);
//			Image che1 = Toolkit.getDefaultToolkit().getImage("pic/che0.png");
//			g.drawImage(che1, 45, 5, 30, 30, this);
			//使用循环
//			String name = "che0";
			//Set、List、Map
			
			
//			int x = 5;
//			int y = 5;
//			int size = 30;
//			int space = 40;
//			int player = 0;
//			Image che0 = Toolkit.getDefaultToolkit().getImage("pic/" + name + ".png");
//			g.drawImage(che0, x + space * 0, y + space * 0, size, size, this);
//			Image che1 = Toolkit.getDefaultToolkit().getImage("pic/" + name + ".png");
//			g.drawImage (che1, x + space * 1, y + space * 0, size, size, this);
//			Image che2 = Toolkit.getDefaultToolkit().getImage("pic/" + name + ".png");
//			g.drawImage(che2, x + space * 2, y + space * 0, size, size, this);
//			for(int i = 0; i < names.length; i++) {
//				Image img = Toolkit.getDefaultToolkit().getImage("pic/" + names[i] + player + ".png");
//				g.drawImage(img, x + space * (xs[i] - 1), y + space * (ys[i] - 1), size, size, this);
//			}
			//使用循环将32个棋子全部画出来
			for(int i = 0; i < chesses.size(); i++) {
				chesses.get(i).draw(g, this);
				System.out.println(chesses.get(i));
			}
			
//			System.out.println(chesses);
		}
		
		/**
		 * 	创建棋子
		 */
		private void initChess() {
			String[] names = {"che", "ma", "xiang", "shi", "boss", "shi", "xiang", "ma", "che", "pao", "pao", "bing", "bing", "bing", "bing", "bing"};
			int[] xs = {1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 8, 1, 3, 5, 7, 9};
			int[] ys = {1, 2, 1, 1, 1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 4, 4};
			for(int i = 0; i < names.length; i++) {
				Chess c = new Chess(names[i], new Point(xs[i], ys[i]), 0);
//				System.out.println(c);
				chesses.add(c);
			}
			for(int i = 0; i < names.length; i++) {
				Chess c = new Chess(names[i], new Point(xs[i], ys[i]), 1);
				c.reserve();
				chesses.add(c);
			}
		}
	}
}
